package com.sanqiu.starters.dubbp2http.registrar;

import cn.hutool.core.util.StrUtil;
import com.sanqiu.starters.dubbp2http.annotation.Dubbo2HttpMethod;
import com.sanqiu.starters.dubbp2http.annotation.Dubbo2HttpService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Method;
import java.util.Objects;

public class Dubbo2HttpBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Dubbo2HttpService annotation = bean.getClass().getAnnotation(Dubbo2HttpService.class);
        if(Objects.isNull(annotation)){
            return bean;
        }
        String serviceName = StrUtil.isEmpty(annotation.serviceName())?bean.getClass().getSimpleName():annotation.serviceName();
        Method[] declaredMethods = bean.getClass().getDeclaredMethods();
        for (Method method : declaredMethods) {
            Dubbo2HttpMethod dubbo2HttpMethod = method.getAnnotation(Dubbo2HttpMethod.class);
            if(Objects.isNull(dubbo2HttpMethod)){
                continue;
            }
            String methodName = StrUtil.isEmpty(dubbo2HttpMethod.name())?method.getName():dubbo2HttpMethod.name();
            if(StrUtil.isEmpty(methodName)){
                continue;
            }
            System.out.println(String.format("开始注册服务:%s 方法:%s", serviceName, methodName));
        }
        return bean;
    }

}
